package entitys

import (
	"rpcSrv/dbs/mysqlGORM"
)

//评论
type Comment struct {
	ID               int    `json:"id"`
	IsReply          int    `json:"is_reply"`     //是否回复别人评论 0否 1是
	ReplyId          int    `json:"reply_id"`     //被回复评论ID  //一级评论ID
	TargetId         int    `json:"target_id"`    //评论目标ID
	FromUid          int    `json:"from_uid"`     //评论人
	ToUid            int    `json:"to_uid"`       //被评论人
	TbName           string `json:"tb_name"`      //评论Target表名
	Status           int    `json:"status"`       //评论状态
	Content          string `json:"content"`      //评论内容
	LikeNumber       int    `json:"like_number"`  //点赞数量
	ReplyNumber      int    `json:"reply_number"` //回复数量
	CreatedTimestamp int64  `json:"created_timestamp"`
	UpdatedTimestamp int64  `json:"updated_timestamp"`
}

func (u *Comment) TableName() string {
	return "comments"
}

const comment_schema = `
create table if not exists comments
(
    id         int(10)      not null auto_increment unique,
    is_reply   tinyint(1)   not null comment '是否回复别人评论 0否 1是',
	reply_id   int(10)      default null comment '被回复评论ID',
    target_id  int(10)      not null comment '评论目标ID',
    from_uid   int(10)      not null comment '评论人',
    to_uid     int(10)      default null '被评论人',
    tb_name    varchar(60)   not null comment '评论Target表名',
    status     tinyint(1) default 1 comment '评论状态',
    content    text not null comment '评论内容',
	like_number int(10) default 0 comment '喜欢数量',
	reply_number int(10) default 0 comment '回复数量',
    created_timestamp int(10)  default 0,
    updated_timestamp int(10)  default 0,
    primary key (id),
    index comment_target_id (target_id) using btree,
	index comment_reply_id (reply_id) using btree,
    index comment_from_uid (from_uid) using btree
) engine = INNODB
  default charset = utf8;
`

func init() {
	db, err := mysqlGORM.GetMysqlDB()
	if err != nil {
		return
	}
	tx := db.Begin()
	defer tx.Commit()
	if !db.HasTable(new(Comment).TableName()) {
		if err = tx.Exec(comment_schema).Error; err != nil {
			tx.Rollback()
			return
		}
	}
}
